// ActionScript file
import assetRegister.components.description;
import commonComponents.*;
import commonComponents.Mask;
import commonComponents.commonFunction;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.resources.ResourceManager;
import mx.rpc.events.ResultEvent;

[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/reset.png")]private var resetIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;
[Embed(source="/images/warning.png")]private var warningIcon:Class;

public var params:Object={};

public function button1_clickHandler(event:MouseEvent):void
{
	Alert.show(commonFunction.getMessages('doYouWantContinue'),commonFunction.getMessages('confirm'),(Alert.YES|Alert.NO),null,onAssetSet,questionIcon);
}
	
public function onAssetSet(event:CloseEvent):void{
	if(event.detail==Alert.YES){		
		params["time"]=new Date();
		params["assetName"]=assetNameTxt.text;
		params["mainCatCode"]=mainCatCombo.selectedItem.mainCatCode;
		params["subCatCode"]=subCatCombo.selectedItem.categoryCode;
		params["status"]='ACT';
		params["location"]=locationCombo.selectedItem.locationCode;
		params["datePurchased"]=descComponent.datePurchase.text;
		params["purchaseOrderNo"]=descComponent.orderNo.text;
		params["warranty"]=descComponent.warrantTxt.text;
		params["invoice"]=descComponent.invoiceTxt.text;
		params["unitCost"]=descComponent.unitCostTxt.text;
		params["vendorName"]=descComponent.vendorNameTxt.text;
		params["vendorContactInfo"]=descComponent.vendorContactTxt.text;		
		params["vendorAddress"]=descComponent.vendorAddressTxt.text;
		params["make"]=descComponent.makeTxt.text;
		params["model"]=descComponent.modelTxt.text;
		params["serialNo"]=descComponent.serialTxt.text;
		params["barcode"]=descComponent.barcodeTxt.text;
		params["quantity"]=descComponent.quantityTxt.text;
		params["length"]=descComponent.lengthTxt.text;
		params["width"]=descComponent.widthTxt.text;
		params["weight"]=descComponent.weightTxt.text;
		params["height"]=descComponent.heightTxt.text;
		params["colour"]=descComponent.colourPicker.selectedColor;		
		params["assetClassCode"]=assetClassCombo.selectedItem.assetClassCode;	
		params["assetSubClassCode"]=assetSubClassCombo.selectedItem.assetSubClassCode;
		params["acquisitionCode"]=accountDistributionTab.acquisitionAccountCode;
		params["dispositionCode"]=accountDistributionTab.dispositionAccountCode;
		if(majorTypeCheckBox.selected){
			params["majorType"]="4";
		}
		else{
			params["majorType"]="1";
		}
		
		if(assetCodeTxt.text.length>0){
			params["assetCode"]=assetCodeTxt.text;
			params["iOrUFlag"]="Update";
		}
		else{
			params["iOrUFlag"]="Insert";
		}		
		assetService.setAssetDetails(params);
	}
}

public function updateBtn_clickHandler(event:MouseEvent):void
{
	Alert.show(commonFunction.getMessages('doYouWantUpdate'),commonFunction.getMessages('confirm'),(Alert.YES|Alert.NO),null,onAssetUpdate,questionIcon);
}

public function onAssetUpdate(event:CloseEvent):void{
	if(event.detail==Alert.YES){		
		params["time"]=new Date();
		params["assetCode"]="";
		params["mainCatCode"]=mainCatCombo.selectedItem.mainCatCode;
		params["subCatCode"]=subCatCombo.selectedItem.categoryCode;
		params["status"]='ACT';
		params["location"]=locationCombo.selectedItem.locationCode;
		params["datePurchased"]=descComponent.datePurchase.text;
		params["purchaseOrderNo"]=descComponent.orderNo.text;
		params["warranty"]=descComponent.warrantTxt.text;
		params["invoice"]=descComponent.invoiceTxt.text;
		params["unitCost"]=descComponent.unitCostTxt.text;
		params["vendorName"]=descComponent.vendorNameTxt.text;
		params["vendorContactInfo"]=descComponent.vendorContactTxt.text;		
		params["vendorAddress"]=descComponent.vendorAddressTxt.text;
		params["make"]=descComponent.makeTxt.text;
		params["model"]=descComponent.modelTxt.text;
		params["serialNo"]=descComponent.serialTxt.text;
		params["barcode"]=descComponent.barcodeTxt.text;
		params["quantity"]=descComponent.quantityTxt.text;
		params["length"]=descComponent.lengthTxt.text;
		params["width"]=descComponent.widthTxt.text;
		params["weight"]=descComponent.weightTxt.text;
		params["height"]=descComponent.heightTxt.text;
		params["colour"]=descComponent.colourPicker.selectedColor;		
		params["assetClassCode"]=assetClassCombo.selectedItem.assetClassCode;
		params["assetSubClassCode"]=assetSubClassCombo.selectedItem.assetSubClassCode;
		params["acquisitionCode"]=accountDistributionTab.acquisitionAccountCode;
		params["dispositionCode"]=accountDistributionTab.dispositionAccountCode;
		if(majorTypeCheckBox.selected){
			params["majorType"]="4";
		}
		else{
			params["majorType"]="1";
		}
		params["iOrUFlag"]="Update";
		assetService.setAssetDetails(params);
	}
}

public function reset():void{
	assetNameTxt.text="";
	mainCatCombo.selectedIndex=-1;
	subCatCombo.selectedIndex=-1;
	subCatCombo.enabled=false;
	locationCombo.selectedIndex=-1;
	assetClassCombo.selectedIndex = -1;
	assetSubClassCombo.selectedIndex = -1;
	majorTypeCheckBox.selected = false;
	descComponent.datePurchase.text="";
	descComponent.orderNo.text="";
	descComponent.warrantTxt.text="";
	descComponent.invoiceTxt.text="";
	descComponent.serialTxt.text="";
	descComponent.unitCostTxt.text="";
	descComponent.vendorNameTxt.text="";
	descComponent.vendorAddressTxt.text="";
	descComponent.vendorContactTxt.text="";
	descComponent.makeTxt.text="";
	descComponent.modelTxt.text="";
	descComponent.barcodeTxt.text="";
	descComponent.quantityTxt.text="";
	descComponent.lengthTxt.text="";
	descComponent.widthTxt.text="";
	descComponent.weightTxt.text="";
	descComponent.heightTxt.text="";
	descComponent.colourPicker.selectedColor=0;
	accountDistributionTab.acquisitionInput.text="";
	accountDistributionTab.dispositionInput.text="";
	accountDistributionTab.acquisitionAccountCode="";
	accountDistributionTab.dispositionAccountCode="";
	accountTabChangedFlag = 0;
}

//on click of  the delete button
protected function deleteAssetRegister_clickHandler(event:MouseEvent):void{
	if (assetRegisterGrid.selectedIndex>=0){
		Alert.show(ResourceManager.getInstance().getString("Message","areYouSure",['Delete']),commonFunction.getMessages('confirm'),3,null,onDelete,questionIcon);
	}
	else{
		Alert.show(ResourceManager.getInstance().getString("Message","selectGridRow",['Delete']),commonFunction.getMessages('warning'),Alert.OK,null,null,warningIcon);
	}
}

protected function onDelete(event:CloseEvent):void{
	if(event.detail==Alert.YES){
		params["time"]=new Date();
		params["assetCode"]=assetRegisterGrid.selectedItem.assetCode;
		assetService.deleteAsset(assetRegisterGrid.selectedItem.assetCode as String);
	}
}
	protected function deleteAsset_resultHandler(event:ResultEvent):void{
		var result:String = event.result as String;
		var index:int = result.search("true");
		var indexFalse:int = result.search("false");	
		if(index!=-1){
			Alert.show(ResourceManager.getInstance().getString("Message","recordSuccess",['Deleted']),commonFunction.getMessages('success'),Alert.OK,null,null,successIcon);
			assetService.getAllAsset();
		}
		else{
			if(result.length>5){
				Alert.show(result.substring(6,result.length),commonFunction.getMessages('error'),Alert.OK,null,null,errorIcon);
			}
			else{
				Alert.show(ResourceManager.getInstance().getString("Message","errorInRecord",['Deletion'])+" "+commonFunction.getMessages('log')
					,commonFunction.getMessages('error'),Alert.OK,null,null,errorIcon);
			}
		}
	}
